package com.xindacloud.sms.adapter.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.adapter.dao.SmsAgentMapper;
import com.xindacloud.sms.adapter.entity.SmsAgent;
import com.xindacloud.sms.adapter.exception.ServiceException;
import com.xindacloud.sms.adapter.service.business.ISmsAgentService;
import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Slf4j
@Service
public class SmsAgentServiceImpl extends ServiceImpl<SmsAgentMapper, SmsAgent> implements ISmsAgentService {

    @Override
    public List<SmsAgent> queryAvailableAgent(String groupId) {
        return lambdaQuery()
                .eq(SmsAgent::getStatus, true)
                .like(SmsAgent::getGroupId, groupId)
                .list();
    }

    @Override
    public SmsAgent queryAgentById(String agentId) {
        SmsAgent smsAgent = lambdaQuery()
                .eq(SmsAgent::getStatus, true)
                .eq(SmsAgent::getAgentId, agentId)
                .one();
        return Optional.ofNullable(smsAgent)
                .orElseThrow(() -> new ServiceException("该服务通道被禁用或不存在.", UnionResultHelp.StatusEnum.NotFound.code()));

    }

    @Override
    public List<SmsAgent> queryAvailableAgentList(List<String> agentIds) {
        final List<SmsAgent> smsAgents = lambdaQuery()
                .eq(SmsAgent::getStatus, true)
                .in(SmsAgent::getAgentId, agentIds)
                .list();
        return Optional.ofNullable(smsAgents)
                .orElseThrow(() -> new ServiceException("没有可用的服务通道.", UnionResultHelp.StatusEnum.NotFound.code()));
    }
}
